<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Echo Try</title>
</head>

<body>
    <input type="text" id="send">
    <input type="button" id="submit" value="send">
    <input type="text" id="receive">
    <script>
        // 回声api，回传你发送的信息
        let ws = new WebSocket('ws://echo.websocket.org')
        ws.onopen = function () {
            console.log('connected')
        }
        ws.onmessage = function (msg) {
            console.log(msg)
            document.getElementById('receive').value = msg.data
        }
        ws.onclose = function () {
            console.log('closed')
        }

        document.getElementById('submit').onclick = function () {
            let msg = document.getElementById('send').value
            ws.send(msg)
        }
    </script>
</body>

</html>